package com.ggh.jinjilive.ui.mall.order.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemOrderGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.order.MineOrderProduct;

import java.util.List;

public class OrderGoodsAdapter extends RecyclerView.Adapter<OrderGoodsAdapter.ItemViewHolder> {


    private List<MineOrderProduct> mineOrderProductList;

    public void setAdd(List<MineOrderProduct> mineOrderProductList) {
        this.mineOrderProductList = mineOrderProductList;
        notifyDataSetChanged();
    }

    /**
     * 更新数据
     */

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOrderGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_order_goods, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
        MineOrderProduct mineOrderProduct = mineOrderProductList.get(position);
        if (null != mineOrderProduct) {
            //商品图片
            Glide.with(holder.binding.riv).load(mineOrderProduct.getImg()).into(holder.binding.riv);
            //商品名称
            holder.binding.tvDescribes.setText(mineOrderProduct.getProduct_name());
            //价格
            holder.binding.tvFee.setText("¥"+mineOrderProduct.getPrice());
            //数量
            holder.binding.tvMagnitude.setText("x" + mineOrderProduct.getNum());
            //规格
            holder.binding.tvSpecification.setText(mineOrderProduct.getProduct_spec_name());

        }
    }


    @Override
    public int getItemCount() {

        return null == mineOrderProductList ? 0 : mineOrderProductList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemOrderGoodsBinding binding;

        public ItemViewHolder(@NonNull ItemOrderGoodsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}